package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.ControlDataFloorHeatingBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.adapter.FloorHeatingDeviceModeValueAdapter;
import com.qiantu.phone.widget.FloorHeatingSeekBar;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.MODE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_OFF;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_ON;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.TEMPERATURE;

/**
 * @Author luzx
 * @Date 2022/1/10
 */
public class FloorHeatingShotView extends BaseDeviceActView {

    private FloorHeatingSeekBar floorHeatingSeekBar;
    private TextView tvModeLabel;
    private RecyclerView modeValueRecyclerView;
    private FloorHeatingDeviceModeValueAdapter mFloorHeatingDeviceModeValueAdapter;
    private ControlDataFloorHeatingBean floorHeatingInfoBean;
    private ControlDataFloorHeatingBean.ControlData controlData;
    private String mode;

    public FloorHeatingShotView(Context context, String type, String deviceType) {
        super(context, type, deviceType);
    }

    @Override
    public int getLayoutId() {
        return R.layout.floor_heating_shot_view;
    }

    @Override
    public void initView() {
        floorHeatingSeekBar = findViewById(R.id.floor_heating_seek_bar);
        tvModeLabel = findViewById(R.id.tv_mode_label);
        modeValueRecyclerView = findViewById(R.id.mode_value_recycler_view);
        floorHeatingSeekBar.isSelectActionMode(true);
        floorHeatingSeekBar.setOnChangeListener(new FloorHeatingSeekBar.OnChangeListener() {

            @Override
            public void onValueChanged(float value) {

            }

            @Override
            public void onStopTrackingTouch(float value) {
//                addAct(POWER, POWER_ON);
                addAct(TEMPERATURE, getTempText(value));
            }

            @Override
            public void onSwitch(boolean isOpen) {
                if(isOpen){
                    addAct(POWER, POWER_ON);
                    floorHeatingSeekBar.setTemp(Float.valueOf(getState(TEMPERATURE, "0")));
//                    addAct(TEMPERATURE, getTempText(floorHeatingSeekBar.getTemp()));
                }else{
                    addAct(POWER, POWER_OFF);
                    removeAct(TEMPERATURE);
                }
                removeAct(MODE);
                mode = defaultValue;
                selectValue(mode);
            }
        });
        floorHeatingInfoBean = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataFloorHeatingBean.class);
        controlData = floorHeatingInfoBean.getControlData();
        floorHeatingSeekBar.setMinMax(controlData.getTemperature_min(), controlData.getTemperature_max());
//        if(controlData.getMode() != null && controlData.getMode().size() > 0){
//            mFloorHeatingDeviceModeValueAdapter = new FloorHeatingDeviceModeValueAdapter(getContext());
//            mFloorHeatingDeviceModeValueAdapter.setData(controlData.getMode());
//            mFloorHeatingDeviceModeValueAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
//                @Override
//                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
//                    if (!floorHeatingSeekBar.isOpen()) {
//                        ToastUtils.show(R.string.device_off_disable);
//                        return;
//                    }
//                    mFloorHeatingDeviceModeValueAdapter.selectItem(position);
//                    DeviceModeValue valueBean = mFloorHeatingDeviceModeValueAdapter.getData().get(position);
//                    mode = valueBean.getValue() + "";
//                    addAct(MODE, mode);
//                }
//            });
//            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
//            linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
//            modeValueRecyclerView.setLayoutManager(linearLayoutManager);
//            modeValueRecyclerView.setAdapter(mFloorHeatingDeviceModeValueAdapter);
//            updateView();
//        }else{
            tvModeLabel.setVisibility(View.GONE);
            modeValueRecyclerView.setVisibility(View.GONE);
//        }
    }

    private String getTempText(float temp){
        String text = null;
        if (temp * 10 % 10 == 0) {
            text = (int) temp + "";
        } else {
            text = temp + "";
        }
        return text;
    }

    private void selectValue(String value){
        if(mFloorHeatingDeviceModeValueAdapter == null || mFloorHeatingDeviceModeValueAdapter.getData() == null){
            return;
        }
        boolean hasSame = false;
        for(int i=0; i<mFloorHeatingDeviceModeValueAdapter.getData().size(); i++){
            if(mFloorHeatingDeviceModeValueAdapter.getData().get(i).getValue() == Integer.valueOf(value)){
                mFloorHeatingDeviceModeValueAdapter.selectItem(i);
                hasSame = true;
                break;
            }
        }
        if(!hasSame){
            mFloorHeatingDeviceModeValueAdapter.clearSelected();
        }
    }

    @Override
    public void updateView() {
        //默认空的话，添加默认动作开
        if(mDeviceActMap.size() == 0){
            addAct(POWER, POWER_ON);
        }
        //根据已保存动作指令集合更新UI
        floorHeatingSeekBar.setOpenState(POWER_ON.equals(getState(POWER, "0")));
        floorHeatingSeekBar.setTemp(Float.valueOf(getState(TEMPERATURE, "0")));
        mode = getState(MODE, defaultValue);
        selectValue(mode);
    }
}
